package com.frame.servlet;

import com.frame.bean.User;

import com.frame.service.UserService;
import com.frame.util.PropUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.List;

@WebServlet(name = "LoginServlet",urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    private static final long servialVersionUID = 1L;
    private UserService userService = new UserService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        String name = req.getParameter("username");
        String psw = req.getParameter("password");
        List<User> list = userService.findUser(name);
        boolean flag = false;
        if(list!=null&&list.size()>0){
            for(User u:list){
                if(u.getPassword().equals(psw)){
                    flag = true;
                    break;
                }
            }
        }
        if(flag)
            req.getRequestDispatcher(PropUtil.getProperty("frame.webapp.jsp_path")+"upload.jsp").forward(req,resp);
        else
        {
            resp.setContentType("text/html;charset=UTF-8");
            PrintWriter out = resp.getWriter();
            out.write("用户名或密码错误<br/>");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
